//package com.example.demo.util;
//
//import com.dashang.masterdatasystem.bean.base.OperLog;
//import com.dashang.masterdatasystem.bean.base.OperationLog;
//import com.dashang.masterdatasystem.dao.log.OperationLogMapper;
//import com.dashang.masterdatasystem.util.ip.IpUtil;
//import com.dashang.masterdatasystem.util.json.JsonMapper;
//import com.fasterxml.jackson.databind.json.JsonMapper;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.AfterReturning;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//
//
//@Component
//@Slf4j
//public class OperationInterceptor {
//    private final JsonMapper jsonMapper = JsonMapper.builder().build();
//
//
//    /**
//     * 设置打印日志切入点
//     */
//    @Pointcut("execution(* com.dashang.masterdatasystem.controller.*..*Controller.*(..))")
//    public void printLogPointCut() {
//    }
//
//    /**
//     * 设置操作日志切入点,记录操作日志,在注解的位置切入代码
//     */
//    @Pointcut("@annotation(com.dashang.masterdatasystem.bean.base.OperLog)")
//    public void operLogPointCut() {
//    }
//
//    @Around("printLogPointCut()")
//    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        HttpServletRequest request = (HttpServletRequest) RequestContextHolder.getRequestAttributes().resolveReference(RequestAttributes
//                .REFERENCE_REQUEST);
//        log.info("===================================");
//        log.info(request.getRemoteAddr() + "@" + request.getRequestURL());
//        log.info("===================================");
//
//        String method = proceedingJoinPoint.getSignature().getName();
//        String action = proceedingJoinPoint.getTarget().getClass().getSimpleName();
//        log.info(String.format("Request: %s.%s  参数: %s", action, method, jsonMapper.toJson(proceedingJoinPoint
//                .getArgs())));
//        long before = System.currentTimeMillis();
//        Object result = proceedingJoinPoint.proceed();
//        log.info(String.format("Response: %s.%s  耗时: %s ms 返回: %s", action, method, System.currentTimeMillis() -
//                before, jsonMapper.toJson(result)));
//        return result;
//    }
//
//    @AfterReturning(value = "operLogPointCut()", returning = "keys")
//    public void saveOperLog(JoinPoint joinPoint, Object keys) {
//        // 获取RequestAttributes
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        // 从获取RequestAttributes中获取HttpServletRequest的信息
//        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes
//                .REFERENCE_REQUEST);
//        OperationLog operlog = new OperationLog();
//        try {
//
//
//            // 从切面织入点处通过反射机制获取织入点处的方法
//            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//            // 获取切入点所在的方法
//            Method method = signature.getMethod();
//            // 获取操作
//            OperLog opLog = method.getAnnotation(OperLog.class);
//            if (opLog != null) {
//                String operModul = opLog.operModul();
//                String operType = opLog.operType();
//                String operDesc = opLog.operDesc();
//                // 操作模块
//                operlog.setOperModul(operModul);
//                // 操作类型
//                operlog.setOperType(operType);
//                // 操作描述
//                operlog.setOperDesc(operDesc);
//            }
//            // 获取请求的类名
//            String className = joinPoint.getTarget().getClass().getName();
//            // 获取请求的方法名
//            String methodName = method.getName();
//            methodName = className + "." + methodName;
//            // 请求方法
//            operlog.setOperMethod(methodName);
//            // 请求方法
//            operlog.setOperRequParam(jsonMapper.toJson(joinPoint.getArgs()));
//            // 返回结果
//            operlog.setOperRespParam(jsonMapper.toJson((keys)));
//            // 请求IP
//            operlog.setOperIp(IpUtil.getIpAddr(request));
//            // 请求URI
//            operlog.setOperUri(request.getRequestURI());
//            oprLogMapper.insert(operlog);
//        } catch (Exception e) {
//            log.error(e.getMessage());
//        }
//    }
//}
